package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    //根据性别查询会员数量
    @Override
    public List<Map<String,Object>> findMemberSexnAndCount() {
        List<Map<String, Object>> memberSexnAndCount = memberDao.findMemberSexnAndCount();
        System.out.println(memberSexnAndCount);
        for (Map<String, Object> map : memberSexnAndCount) {
            Object name = map.get("name");
            if (name.equals("1")){
                map.put("name","男");
                System.out.println(memberSexnAndCount);
            }else {
                map.put("name","女");
                System.out.println(memberSexnAndCount);
            }
        }
        return memberSexnAndCount;
    }

    @Override
    public Map<String, Object> getMemberAge(String year) {
        //将年份转化为int
        int yearCount = Integer.parseInt(year);
        //Map集合封装查询日期数据
        Map<String,String> date = new HashMap<>();
        //List集合封装年龄段数据
        List<String> ageBracket =  new ArrayList<>();
        //List<Map>集合封装name,value
        List<Map<String, Object>> nameAndValueList = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            yearCount = yearCount=20*(i-1);
            int changeYear = yearCount-20*i;
            int beginAge = 0 + 20*(i-1);
            int endAge = 0 + 20*i;
            //拼接年龄段字符串
            String age = beginAge+"-"+endAge;
            //年龄段存入年龄段List集合
            ageBracket.add(age);
            String begin = changeYear+"-1-1";
            String end = yearCount+"-12-31";
            date.put("begin",begin);
            date.put("end",end);
            //name,value封装存入List集合的Map
            Map<String, Object> property = new HashMap<>();
            Integer ageCount = memberDao.findCountByDate(date);
            //将年龄段，查询数据存入Map集合
            property.put("name",age);
            property.put("value",ageCount);
            //将Map存入List集合
            nameAndValueList.add(property);
        }
        //Map集合封装前台响应数据
        Map<String,Object> pieChartData = new HashMap<>();
        pieChartData.put("memberAge",ageBracket);
        pieChartData.put("memberAgeCount",nameAndValueList);
        return pieChartData;
    }
}
